<?php

use App\Util\Helper;
use Plugins\AdminBasic\Enums\BasicAdministratorsEnumStatus;
use Plugins\AdminBasic\Http\Admin\Controller\AdministratorsController;
use Plugins\AdminBasic\Http\Admin\Controller\RoleController;
use Plugins\AdminBasic\Models\BasicAdministrators;
use Sc\Util\HtmlStructure\Form;
use Sc\Util\HtmlStructure\Form\FormItem;
use Sc\Util\HtmlStructure\Html\Html;

Html::create('添加管理员');

$form = Form::create('administrators')->config('label-width', '80px');

$form->addFormItems(
    FormItem::hidden('id', ''),
    FormItem::text('name', '用户名'),
    FormItem::text('account', '账号'),
    FormItem::password('password', '密码')->setWhen('!administrators.id'),
    FormItem::radio('status', '状态')->options(BasicAdministratorsEnumStatus::formOptions()),
    FormItem::select('role_ids', '角色')->multiple()
        ->remoteGetOptions(sc_route([RoleController::class, 'listsData']), valueName: 'id', labelName: 'name'),
    FormItem::submit()->successClose('current')->submitUrl(
        Helper::route()->to([AdministratorsController::class, 'store'])->url(),
        Helper::route()->to([AdministratorsController::class, 'update'])->url(),
    )->success('VueApp.administratorsGetData()')
);

if(Helper::request()->query('id')){
    $data = BasicAdministrators::find(Helper::request()->query('id'))?->toArray() ?: [];
    $form->setData($data);
}
return $form->render();